package com.vanhon.engine.decrypt;

import android.net.Uri;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;

/* loaded from: classes.dex */
class MyHttpRequestHandler implements HttpRequestHandler {
    private static final String TAG = "MyHttpRequestHandler";

    @Override // org.apache.http.protocol.HttpRequestHandler
    public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
        String uri = httpRequest.getRequestLine().getUri();
        if (uri != null && uri.length() > 1) {
            uri = uri.substring(1);
        }
        int[] iArr = {-1};
        InputStream prepareStream = prepareStream(uri, iArr);
        Log.e(TAG, "handle, url: " + uri + ", length: " + iArr);
        if (prepareStream != null) {
            try {
                DecryptStream create = DecryptStream.create(uri);
                MyStreamEntity myStreamEntity = new MyStreamEntity(create, create.getLength());
                httpResponse.setStatusCode(200);
                httpResponse.setHeader("Content-Type", "video/*");
                httpResponse.setEntity(myStreamEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private InputStream prepareStream(String str, int[] iArr) throws IOException {
        String str2;
        String str3;
        InputStream fileInputStream;
        int indexOf = str.indexOf(58);
        if (indexOf != -1) {
            str2 = str.substring(0, indexOf);
            str3 = str.substring(indexOf + 1);
        } else {
            str2 = "";
            str3 = str;
        }
        if (str2 != null && str3 != null) {
            if (str2.equalsIgnoreCase(HttpHost.DEFAULT_SCHEME_NAME)) {
                Log.e(TAG, "prepareStream, uri: " + str);
                HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
                fileInputStream = httpURLConnection.getInputStream();
                iArr[0] = httpURLConnection.getContentLength();
            } else {
                File file = new File(Uri.decode(str3));
                if (!file.exists() || !file.canRead() || !file.isFile()) {
                    Log.e(TAG, "prepareStream error, exists: " + file.exists() + ", canRead: " + file.canRead() + ", isFile: " + file.isFile() + ", path: " + str3);
                    return null;
                }
                iArr[0] = (int) file.length();
                fileInputStream = new FileInputStream(file);
                Log.e(TAG, "file======================length: " + iArr);
            }
            return fileInputStream;
        }
        Log.e(TAG, "prepareStream, invalid url: " + str);
        return null;
    }

    /* loaded from: classes.dex */
    private static class MyStreamEntity extends InputStreamEntity {
        public MyStreamEntity(InputStream inputStream, long j) {
            super(inputStream, j);
        }

        @Override // org.apache.http.entity.InputStreamEntity, org.apache.http.entity.AbstractHttpEntity, org.apache.http.HttpEntity
        public void consumeContent() {
            Log.e(MyHttpRequestHandler.TAG, "consumeContent================");
            try {
                InputStream content = getContent();
                if (content != null) {
                    content.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
